/**
 * Copyright (c) 2019-2029, wwww.kiwipeach.cn (liuburu@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * https://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cn.kiwipeach.blog.controller.common;

import cn.kiwipeach.blog.base.AjaxResponse;
import cn.kiwipeach.blog.domain.UserInfoVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import open.kiwipeach.autoconfigure.shiro.token.AccessToken;
import open.kiwipeach.autoconfigure.web.utils.UserUtil;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 博客系统基础控制器
 *
 * @author kiwipeach
 * @since 1.0
 */
//@Api(tags = "03通用模块")
@Controller
@Slf4j
public class CommonSysController {

    /**
     * 获取当前登陆用户信息
     *
     * @return 返回当前登陆用户信息
     */
    @ApiOperation(value = "当前登录用户查询接口", notes = "一般用于获取当前登录用户信息。")
    @GetMapping("currentUser")
    @ResponseBody
    public AjaxResponse<UserInfoVO> queryUserInfo() {
        // token信息会被JWTFilter拦截器拦截并解析
        AccessToken currentUser = UserUtil.getCurrentUser();
        if (currentUser == null) {
            return AjaxResponse.success(null);
        } else {
            //FIXME openid建议不要跟id一致，后期在做处理了。
            UserInfoVO userInfoVO = new UserInfoVO(currentUser.getUserName(), currentUser.getNickName(), currentUser.getHeadUrl(), currentUser.getId());
            userInfoVO.setRolePermissions(currentUser.getRolePermissions());
            return AjaxResponse.success(userInfoVO);
        }
    }


}
